package com.e3.toolkit;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import freemarker.template.SimpleHash;

public class MessagesGMain extends JFrame {

	private static final long serialVersionUID = 1430648923318022394L;
	
	private JPanel centerPanel = new JPanel(null);

	private JTextField  msgsFile = new JTextField();
	
	private JToolBar toolBar = new JToolBar();
	private TransferAction transferAction = new TransferAction(this);
		
	public MessagesGMain(){
		init();
	}
	private void init(){
		this.setTitle("Tools for messages");
		

		createField("messages文件", 0, msgsFile);	
				
		toolBar.add(transferAction);
		
		this.add(toolBar,BorderLayout.NORTH);
		this.getContentPane().add(centerPanel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setPreferredSize(new Dimension(800,600));
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((int)screenSize.getWidth()/2-400, (int)screenSize.getHeight()/2-300);
		
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosed(WindowEvent e) {
				System.exit(0);
			}
			
		});
		
	}
	private void createField(String name,int row, JComponent field){
		JLabel label = new JLabel(name);
		label.setHorizontalTextPosition(JLabel.RIGHT);
		//label.setPreferredSize(new Dimension(40,27));
		label.setBounds(20,30*(row + 1)+10,100,27);
		//field.setPreferredSize(new Dimension(120,27));
		field.setBounds(140, 30*(row + 1)+10, 220, 27);
		
		centerPanel.add(label);
		centerPanel.add(field);
	}
	private class TransferAction extends AbstractAction{
		private static final long serialVersionUID = -3681374552218717937L;
		
		public TransferAction(JFrame frame) {
			super("Generate");
		}

		public void actionPerformed(ActionEvent e) {
			onTransfer();
		}
		
	}
	
	private void onTransfer(){
		String fileName = msgsFile.getText();
		
		if(fileName==null||fileName.length()==0){
			JOptionPane.showMessageDialog(this, "请输入包名和类名");
			return;
		}
		
		File file = new File(fileName);
		Properties props = new Properties();
		try {
			props.load(new InputStreamReader(new FileInputStream(file),"utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		String javaFileName = fileName.replaceAll("_zh.properties", "");
		String pgName = fileName.substring(fileName.indexOf("\\com"),fileName.lastIndexOf("\\"));
		pgName = pgName.substring(1);
		pgName = pgName.replace('\\', '.');
		
		String className = javaFileName.substring(javaFileName.lastIndexOf("\\")+1);
		javaFileName = javaFileName+".java";
		
		ArrayList<Object> keys = Collections.list(props.keys());
		List<String> sortKeys = new ArrayList<String>();
		for (Object object : keys) {
			sortKeys.add((String)object);
		}
		Collections.sort(sortKeys);
		MessageMeta msgMeta = new MessageMeta(props);
		SimpleHash sh = new SimpleHash();
		sh.put("className",className);
		sh.put("keys", sortKeys);
		sh.put("packageName", pgName);
		sh.put("MsgMeta", msgMeta);
		
		String ftl = "messagessub.ftl";
		if(javaFileName.indexOf("CommonMessages")>=0){
			ftl = "messages.ftl";
		}
		TransferUtils.transfer(ftl, javaFileName , sh);
	}
	
	
	public static void main(String[] args){
		MessagesGMain frame = new MessagesGMain();
		frame.pack();
		frame.setVisible(true);
	}
}
